const app = getApp();
const db = wx.cloud.database()    //初始化数据库环境
const recordTemp = db.collection('recordTemp')
var util = require('../../utils/util.js')
var time
var date
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ColorList: app.globalData.ColorList,
    userInfo: '',
    time: '',
    date: '',
    name: '',
    temp: '',
    index: 0,
    picker: ['正常', '发热', '咳嗽', '腹泻', '感冒', '头晕头疼'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    var userInfo;
    let realtime = util.formatTime(new Date());
    date = realtime.substring(0, 4) + "-" + realtime.substring(5, 7) + "-" + realtime.substring(8, 10)
    time = realtime.substring(11, 16)
    wx.getStorage({
      key: 'userInfo',
      success: function (res) {
        that.setData({
          userInfo: res.data
        })
      },
    })
    //填充表单
    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          name: res.data[0],
        })
      },
    })
    that.setData({
      date: date,
      time: time,
      userInfo: app.globalData.userInfo
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  TimeChange(e) {
    this.setData({
      time: e.detail.value
    })
  },
  DateChange(e) {
    this.setData({
      date: e.detail.value
    })
  },
  PickerChange(e) {
    console.log(e);
    this.setData({
      index: e.detail.value
    })
  },
  //接收用户输入的姓名
  inputName: function (e) {
    this.setData({
      name: e.detail.value,
    })
  },
  //接收用户体温
  inputTemp: function (e) {
    this.setData({
      temp: e.detail.value,
    })
  },
  //接收用户工作单位
  inputOrganization: function (e) {
    this.setData({
      organization: e.detail.value,
    })
  },
  /**
   * 上传用户的体温信息
   */
  tijiao: function () {
    let that = this
    //添加必要的输入是否为空判断
    if (that.data.name == '') {
      wx.showModal({
        title: '提示!',
        content: '请输入姓名',
      })
    } else if (that.data.temp == '') {
      wx.showModal({
        title: '提示!',
        content: '请输入您当前体温',
      })
    } else {
      recordTemp.add({
        data: {
          date: that.data.date,
          time: that.data.time,
          name: that.data.name,
          health: that.data.index,
          temp: that.data.temp,
          organization: that.data.organization
        },
        success:function(res) {
          // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
          // console.log(res)
          // wx.hideLoading();
          wx.showToast({
            title: '录入成功',
            icon: 'success',
            duration:2000,
            succes:function(){
              console.log("显示tosta成功")
              wx.navigateBack({   //成功后跳转回原页面
              url: '../home'
            })
            },
            fail:function(){
                console.error
            },
            complete:function(){
              console.log(11111111111111)
              // wx.navigateBack({   //成功后跳转回原页面
              //   url: '../home'
              // })
            }
          })
          setTimeout(function () {
            wx.navigateBack({   //成功后跳转回原页面
                url: '../home'
              })
          }, 2000)
        }
      })
    }
  }
})